//
//  JKWebViewController.m
//  JKRecover
//
//  Created by Veer on 2018/7/16.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "JKWebViewController.h"

#import <JavaScriptCore/JavaScriptCore.h>

@interface JKWebViewController () <UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, strong) JSContext *jsContext;

@end

@implementation JKWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"加载中...";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupSubView];
    if (self.urlStr) {
        [self loadUrlWith:self.urlStr];
    }
}

- (void)setupSubView
{
    
    UIWebView *webView = [[UIWebView alloc] init];
    webView.delegate = self;
    webView.frame = self.view.bounds;
    if (@available(iOS 11.0, *)) {
        
    }else{
        webView.scrollView.contentInset = UIEdgeInsetsMake(STATUS_BAR_HEIGHT, 0, 0, 0);
    }
    [self.view addSubview:webView];
    self.webView = webView;
}

- (void)loadUrlWith:(NSString *)urlStr
{
    
    VRLog(@"------------url------------:%@", urlStr);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlStr]];
    [self.webView loadRequest:request];
}

- (void)createJSBridge
{
//    JSContext *jsContext = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//    self.jsContext = jsContext;
//    weakify(self)
//    jsContext[@"onBack"] = ^(NSString *card){
//        
//        strongify(self)
//        NSLog(@"------------upLoadAddressBook card:%@", card);
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            [self.navigationController popViewControllerAnimated:YES];
//        });
//    };
}


#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"----------webViewDidStartLoad----url:%@", webView.request.URL.absoluteString);
    [self createJSBridge];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"----------webViewDidFinishLoad----url:%@", webView.request.URL.absoluteString);
    [self createJSBridge];
    if (self.titleStr.length == 0) {
        
        NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        NSArray *titleArray = nil;
        if (title.length > 0) {
            titleArray = [title componentsSeparatedByString:@"-"];
            if (titleArray.count > 0) {
                self.title = [titleArray lastObject];
            }else{
                self.title = title;
            }
        }
    }else{
        
        self.title = self.titleStr;
    }
}

//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
//{
//
//    NSString *urlString = request.URL.absoluteString;
//    NSLog(@"urlStrig : %@", urlString);
//    if (urlString.length > 6 && [[urlString substringToIndex:6] isEqualToString:@"taobao"]) {
//
//        // 打开支付宝
//        NSURL *url = [NSURL URLWithString:urlString];
//        if (@available(iOS 10.0, *)) {
//            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
//                if (!success) {
//                    NSLog(@"未安装淘宝");
//                }
//            }];
//        } else {
//            if ([[UIApplication sharedApplication] canOpenURL:url]) {
//                [[UIApplication sharedApplication] openURL:url];
//            } else {
//                NSLog(@"未安装淘宝");
//            }
//        }
//    }
//
//    return YES;
//}

@end
